<?php get_instance()->view('common/header');?>
<style type="text/css">
.ul_purview li .div_title {
    float: left;
    padding-top: 7px;
}
.ul_purview li .function-box {
    float: right;
    margin-left: 5px;
    width: 450px;
}
.ul_purview li{float:left;width:100%;}
.module-title{vertical-align: middle !important; text-align: center;font-size:15px;}
.radio-inline, .checkbox-inline {
    cursor: pointer;
    display: inline-block;
    font-weight: 400;
    margin-bottom: 0;
    padding-left: 0;
    vertical-align: middle;
}
</style>
<div class="page-container home">
  <?php get_instance()->view('common/sidebar');?>
  <div class="page-content-wrapper">
		<div class="page-content">
			<div class="row">
				<div class="col-md-12">
					<!-- BEGIN PAGE TITLE & BREADCRUMB-->
					<h3 class="page-title"> <?php echo $is_add ? '新增' : '编辑';?> </h3>
					<ul class="page-breadcrumb breadcrumb">
						<li class="btn-group"><a class="btn default ml_5 return"
							href="<?php echo getUrl('setting/role');?>"><i class="fa fa-chevron-left"></i> 返回</a></li>
						<li><i class="fa fa-home"></i><a href="<?php echo getUrl('setting/role');?>">角色管理 </a> <i class="fa fa-angle-right"></i></li>
						<li><?php echo $is_add ? '新增' : '编辑';?></li>
					</ul>
				</div>
			</div>
			<div class="row">
				<div class="col-md-12">
					<!-- BEGIN VALIDATION STATES-->
					<div class="portlet-body form order">
						<!-- BEGIN FORM-->
						<form action="" class="form-horizontal" method="post" id="create_forms">
							<div class="form-body" style="margin-top: 0;margin-bottom: 20px;">
								<div class="row">
									<div class="col-md-12" style="margin: 0 auto; float: none;">
										<div class="form-group">
											<label class="control-label col-md-3">职位名称 ：<span
												class="required">*</span></label>
											<div class="col-md-4">
												<input type="text" class="form-control required" name="name" value="<?php echo $is_add ? '' : $info['name'];?>" />
											</div>
										</div>
										<div class="form-group">
											<label class="control-label col-md-3">权限 ：</label>
											<div class="col-md-6">
												<table class="table table-bordered form-horizontal">
													<tbody>
														<tr>
															<td>
																<ul class="ul_purview">
																<?php foreach ($menu as $menuK => $menuV): if( ! isset($menuV['permissions']) && ! isset($menuV['sub_menu'])) continue;?>
																	<li>
																		<?php $k = $menuK;?>
																		<div class="div_title"><strong><?php echo $menuV['menu_name']?></strong>&nbsp;&nbsp;</div>
																		<div class="function-box">
																		<?php if( ! isset($menuV['sub_menu'])):?>
																			<?php 
																				foreach ($menuV['permissions'] as $pK => $pV):
																			?>
																				<label class="checkbox-inline"> <input <?php if( ! $is_add && isset($info['permissions'][$k]) && is_array($info['permissions'][$k]) && in_array($pK, $info['permissions'][$k])):?>checked="true"<?php endif;?>  class="power-checked" rely-on="<?php echo implode(',', $pV['rely_on']);?>" name="p[<?php echo $k;?>][]" value="<?php echo $pK;?>" type="checkbox"><?php echo $pV['name']?></label>
																			<?php endforeach;?>
																		<?php else:?>
																			&nbsp;	
																		<?php endif;?>
																		</div>
																	</li>
																	<?php if(isset($menuV['sub_menu'])):?>
																		<?php foreach ($menuV['sub_menu'] as $subK =>$subV):?>
																			<li>
																				<div class="div_title">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $subV['menu_name']?></div>
																				<div class="function-box">
																				<?php 
																					foreach ($subV['permissions'] as $pK =>$pV):
																				?>
																					<label class="checkbox-inline"> <input <?php if( ! $is_add && isset($info['permissions'][$k]) && is_array($info['permissions'][$k]) && in_array($subK.'-'.$pK, $info['permissions'][$k])):?>checked="true"<?php endif;?> class="power-checked" rely-on="<?php echo implode(',', $pV['rely_on']);?>" name="p[<?php echo $k;?>][]" value="<?php echo $subK.'-'.$pK;?>" type="checkbox"><?php echo $pV['name']?></label>
																				<?php endforeach;?>
																				</div>
																			</li>
																		<?php endforeach;?>
																	<?php endif;?>
																	<?php endforeach;?>
																</ul>
															</td>
														</tr>
													</tbody>
												</table>
												<script type="text/javascript">
													$(function(){
														$('.power-checked').change(function(){

															var thisVal = $(this).val();
															var p = $(this).parents('.function-box');
															var relyOn = ','+$(this).attr('rely-on')+',';

															if($(this).attr('checked')){
																if(relyOn){
																	p.find('.power-checked').each(function(){
																		var v = $(this).val().split('-');	
																		v = v[v.length - 1];
																		if(relyOn.indexOf(','+v+',') > -1){
																			$(this).attr('checked', true).uniform();		
																		}
																	});
																}
															}else{
																p.find('.power-checked').each(function(){
																	var v = thisVal.split('-');	
																	v = v[v.length - 1];
																	var relyOn = ','+$(this).attr('rely-on')+',';	
																	if(relyOn.indexOf(','+v+',') > -1){
																		$(this).attr('checked', false).uniform();		
																	}
																});
															}
														});	
													});
												</script>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="form-actions fluid hide"
								style="position: fixed; z-index: 10000; bottom: 0px">
								<div class="col-md-offset-3 col-md-8">
									<input name="id" type="hidden" value="<?php echo ! empty($info) ? $info['id'] : ''; ?>" />
									<button class="btn green submit-btn" type="submit">
										<i class="fa fa-save"></i> 保存
									</button>
									<a class="btn default" href="<?php echo getUrl('setting/role');?>"><i
										class="fa fa-chevron-left"></i> 返回</a>
								</div>
							</div>
							<script type="text/javascript">
				              	 $(function(){
				                  	setTimeout("$('.form-actions').width($('.form-body').innerWidth()).removeClass('hide');", '500');
				              		window.onresize = function(){
										$('.form-actions').width($('.form-body').innerWidth());
				                  	}	
				                 });
				              </script>
						</form>
						<!-- END FORM-->
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(function(){
		$.e_validate.init('#create_forms',{});
		$('select').select2();
	});
</script>
</body>
</html>